package linuxbox.bean;

import java.lang.reflect.*;
import org.apache.log4j.Logger;

import linuxbox.util.*;
import linuxbox.exception.*;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2002</p>
 * <p>Company: </p>
 * @author unascribed
 * @version 1.0
 */

public class DBField
{
  protected static Logger logger = Logger.getLogger(DBField.class.getName());

  public static final int BYTE_TYPE = 1;
  public static final int CHAR_TYPE = 2;
  public static final int SHORT_TYPE = 3;
  public static final int INT_TYPE = 4;
  public static final int LONG_TYPE = 5;
  public static final int FLOAT_TYPE = 6;
  public static final int DOUBLE_TYPE = 7;
  public static final int STRING_TYPE = 8;
  public static final int BOOLEAN_TYPE = 9;
  public static final int TIMESTAMP_TYPE = 10;
  public static final int DATE_TYPE = 11;
  public static final int TIME_TYPE = 12;

  protected String fieldName;
  protected int fieldType;
  protected String defaultValue;
  protected Field field;

  /**
   * Is this a key field?
   */
  protected boolean isKey = false;

  /**
   * Is this a AutoIncrement field?
   */
  protected boolean isAutoIncrement = false;

  /**
   * Is a null value allowed for this field.
   */
  protected boolean allowNull;

  /**
   * The user friendly description of this field.
   */
  protected String Description;

  /**
   * Constructor:
   * @param    myName Field name of this field
   * @param    myIsKey True if null is allowed, false if not
   * @param    myAllowNull True if null is allowed, false if not
   * @param    myDescrip Description (title) of the field
   * @throws  DBException
   */
  public DBField(String myName, int myFieldType, boolean myIsKey, boolean myAllowNull, String myDefault, String myDescrip)
  {
    fieldName = myName;
    fieldType = myFieldType;
    allowNull = myAllowNull;
    isKey = myIsKey;
    defaultValue = myDefault;
    Description = myDescrip;
  }

  public void setupField(Field f)
  {
    field = f;
  }

  public void getValueFromDb(Object o, DbAdapter db)
  {
    try
    {
      switch (fieldType)
      {
        case DBField.BYTE_TYPE:
          field.setByte(o, db.getByte(fieldName));
          break;

        case DBField.CHAR_TYPE:
          field.setChar(o, db.getString(fieldName).charAt(0));
          break;

        case DBField.SHORT_TYPE:
          field.setShort(o, (short)db.getInt(fieldName));
          break;

        case DBField.INT_TYPE:
          field.setInt(o, db.getInt(fieldName));
          break;

        case DBField.LONG_TYPE:
          field.setLong(o, db.getLong(fieldName));
          break;

        case DBField.FLOAT_TYPE:
          field.setFloat(o, db.getFloat(fieldName));
          break;

        case DBField.DOUBLE_TYPE:
          field.setDouble(o, db.getDouble(fieldName));
          break;

        case DBField.STRING_TYPE:
          field.set(o, db.getString(fieldName));
          break;
        case DBField.BOOLEAN_TYPE:
          field.setBoolean(o, db.getInt(fieldName)!=0);
          break;

        case DBField.TIMESTAMP_TYPE:
          field.set(o, db.getTimestamp(fieldName));
          break;

        case DBField.DATE_TYPE:
          field.set(o, db.getDate(fieldName));
          break;

        case DBField.TIME_TYPE:
          field.set(o, db.getTime(fieldName));
          break;

        default:
          logger.error("Field type not defined:" + fieldType);
          throw new LinuxboxException("Field type not defined:" + fieldType);
      }
    }
    catch (Exception e)
    {
      logger.error("Error getFieldValueFromDb:" + o + " Field:" + fieldName);
    }
  }

  public String getFieldName()
  {
    return fieldName;
  }

  public int getFieldType()
  {
    return fieldType;
  }

  public String getDefaultValue()
  {
    return defaultValue;
  }

  public Field getField()
  {
    return field;
  }

  public boolean getIsKey()
  {
    return isKey;
  }

  public boolean getAllowNull()
  {
    return allowNull;
  }

  public String getDescription()
  {
    return Description;
  }

  public boolean getAutoIncrement()
  {
    return isAutoIncrement;
  }

  public void setAutoIncrement(boolean b)
  {
    isAutoIncrement = b;
  }
}

